package com.fqcar.fqdf.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.fqcar.fqdf.R;
import com.fqcar.fqdf.entity.CarDetailEntity;
import com.fqcar.fqdf.util.NavigationUtils;
import com.fqdf.framework.android.adapter.BaseRecycleAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SearchCarListAdapter extends BaseRecycleAdapter<CarDetailEntity,SearchCarListAdapter.ViewHolder> {

    private Context context;
    public SearchCarListAdapter(Context context) {
        super(context);
        this.context = context;
    }

    /*@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.adapter_car_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final CarDetailEntity carDetailEntity = getItem(position);
        holder.mainCarPlateNum.setText(carDetailEntity.getLicense());
        holder.mainCarBrand.setText(carDetailEntity.getBrand());
        holder.mainCarState.setText(carDetailEntity.getOnListingDesc());
        holder.mainCarMileage.setText(carDetailEntity.getMileageNumberDesc());
        holder.mainCarBattery.setText(carDetailEntity.getSocNumberDesc());
        holder.mainCarChargeState.setText(carDetailEntity.getIsChargingDesc());
        holder.mainCarMachineState.setText(carDetailEntity.getStateOnlineDesc());
        holder.mainCarStateDesc.setText(carDetailEntity.getOnListingDesc());
        if(carDetailEntity.getLastDeal() != null){
            holder.mainCarStateDescTime.setText(carDetailEntity.getLastDeal().getEndTime());
        }

        holder.navBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NavigationUtils.showNavigationDialog(context, new LatLng(carDetailEntity.getLat(), carDetailEntity.getLng()),
                        NavigationUtils.Driving, NavigationUtils.ReturnCar, "车辆位置");

            }
        });

        return convertView;
    }*/

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(getLayoutInflater().inflate(R.layout.adapter_car_list, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        final CarDetailEntity carDetailEntity = getItem(position);
        holder.mainCarPlateNum.setText(carDetailEntity.getCarNum());
        holder.mainCarBrand.setText(carDetailEntity.getBrand());
        holder.mainCarState.setText(carDetailEntity.getStatusDesc());
        holder.mainCarMileage.setText(carDetailEntity.getRemainMileageDesc());
        holder.mainCarBattery.setText(carDetailEntity.getPowerDesc());
        holder.mainCarChargeState.setText(carDetailEntity.getChargeStatusDesc());
        holder.mainCarMachineState.setText(carDetailEntity.getOnlineStatusDesc());
        holder.mainCarStateDesc.setText(carDetailEntity.getStatusDesc());
        if(carDetailEntity.getLastDeal() != null){
            holder.mainCarStateDescTime.setText(carDetailEntity.getLastDeal().getStartTime());
        }

        holder.navBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NavigationUtils.showNavigationDialog(context, new LatLng(carDetailEntity.getLatitude(), carDetailEntity.getLongitude()),
                        NavigationUtils.Driving, NavigationUtils.ReturnCar, "车辆位置");

            }
        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.main_car_plate_num)
        TextView mainCarPlateNum;
        @BindView(R.id.main_car_brand)
        TextView mainCarBrand;
        @BindView(R.id.main_car_state)
        TextView mainCarState;
        @BindView(R.id.main_car_mileage)
        TextView mainCarMileage;
        @BindView(R.id.main_car_battery)
        TextView mainCarBattery;
        @BindView(R.id.main_car_charge_state)
        TextView mainCarChargeState;
        @BindView(R.id.main_car_machine_state)
        TextView mainCarMachineState;
        @BindView(R.id.main_car_state_desc)
        TextView mainCarStateDesc;
        @BindView(R.id.main_car_state_desc_time)
        TextView mainCarStateDescTime;
        @BindView(R.id.nav_btn)
        Button navBtn;
        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
